﻿Namespace DB
    Public Class DBFieldList
        Inherits System.ComponentModel.BindingList(Of DBField)

        Private _OwningDBTable As DBTable

        Sub New(ByVal OwningDBTable As DBTable)
            _OwningDBTable = OwningDBTable
        End Sub

        ''' <summary>Gets or sets the element at the specified index.</summary>
        ''' <param name="index">The zero-based index of the element to get or set.</param>
        Default Public Shadows Property Item(ByVal index As Integer) As DBField
            Get
                Return MyBase.Item(index)
            End Get
            Set(ByVal value As DBField)
                MyBase.Item(index) = value
            End Set
        End Property

        ''' <summary>Gets or sets the element with the specified name</summary>
        ''' <param name="FieldName">The fieldname of the element to get or set.</param>
        ''' <exception cref="IndexOutOfRangeException">Thrown if the specified FieldName is not found.</exception>
        Default Public Shadows Property Item(ByVal FieldName As String) As DBField
            Get
                For Each tmpItem As DBField In Me
                    If tmpItem.DBfieldName.Equals(FieldName, StringComparison.OrdinalIgnoreCase) Then
                        Return tmpItem
                    End If
                Next
                Throw New IndexOutOfRangeException(String.Format("'{0}' is not an element of the list.", FieldName))
            End Get
            Set(ByVal value As DBField)
                For Each tmpItem As DBField In Me
                    If tmpItem.DBfieldName.Equals(FieldName, StringComparison.OrdinalIgnoreCase) Then
                        tmpItem = value
                        Exit Property
                    End If
                Next
                Throw New IndexOutOfRangeException(String.Format("'{0}' is not an element of the list.", FieldName))
            End Set
        End Property

        ''' <summary>Checks if the specified fieldname is in the fieldlist</summary>
        ''' <param name="FieldName">The fieldname to look for.</param>
        ''' <returns>true if a field with that name is in the list, false if not</returns>
        Public Function ContainsFieldName(ByVal FieldName As String) As Boolean
            Try
                Return Me(FieldName) IsNot Nothing
            Catch ex As IndexOutOfRangeException
                Return False
            End Try
        End Function

        ''' <summary>Checks if the specified field is in the fieldlist</summary>
        ''' <param name="DBfieldID">The Oracle field sequence to look for.</param>
        ''' <returns>true if a field with that name is in the list, false if not</returns>
        Public Function ContainsFieldID(ByVal DBfieldID As Integer) As Boolean
            For Each Item As DBField In Me
                If Item.DBfieldID = DBfieldID Then
                    Return True
                End If
            Next
            Return False
        End Function

        ''' <summary>Checks if the specified field is in the fieldlist</summary>
        ''' <param name="DBfieldID">The Oracle field sequence to look for.</param>
        ''' <returns>The DB field with the specified ID or nothing if not found</returns>
        Public Function GetFieldByID(ByVal DBfieldID As Integer) As DBField
            For Each Item As DBField In Me
                If Item.DBfieldID = DBfieldID Then
                    Return Item
                End If
            Next
            Return Nothing
        End Function

        Protected Overrides Sub SetItem(ByVal index As Integer, ByVal item As DBField)

            MyBase.SetItem(index, item)
        End Sub

    End Class
End Namespace

